package cn.weyoungstudio.hwes.dao;

import cn.weyoungstudio.hwes.domain.User;
import cn.weyoungstudio.hwes.jdbc.common.SQLParameter;
import cn.weyoungstudio.hwes.jdbc.session.SqlSession;
import cn.weyoungstudio.hwes.jdbc.session.SqlSessionFactory;

import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    private static SqlSessionFactory sqlSessionFactory = SqlSessionFactory.createSqlSessionFactory();

    public int add(User user) throws SQLException {
        SqlSession sqlSession = sqlSessionFactory.openSession();

        String sql = "insert into t_user(user_no, password, name, clazz_name) values(?,?,?,?)";
        List<SQLParameter> params = new ArrayList<>();
        params.add(new SQLParameter(user.getUserNo(), Types.VARCHAR));
        params.add(new SQLParameter(user.getPassword(), Types.VARCHAR));
        params.add(new SQLParameter(user.getName(), Types.VARCHAR));
        params.add(new SQLParameter(user.getClazzName(), Types.VARCHAR));
        int rows = sqlSession.executeUpdate(sql, params);
        return rows;
    }
}